package any.incubator;

import java.util.Iterator;
import java.util.Map.Entry;

import any.AnyVisitor;
import any.SequentialAny;
import any.impl.util.Utils;

public abstract class BaseShallowSequentialAny extends BaseSequentialAny {
	
	public Iterable<? extends Entry<String, ? extends SequentialAny>> entries() {
		reset();
	    return Utils.wrapIterator(new EntriesIterator());
	}
	
	@Override
	public boolean accept(AnyVisitor<SequentialAny> visitor) throws NullPointerException {
		if (visitor.beginVisit(null, 0, null)) {
			int i = 0;
			for (Entry<String, ? extends SequentialAny> entry : entries()) {
	            if (!visitor.visit(entry.getKey(), i, entry.getValue())) break;
	            i++;
            }			
		}
		visitor.endVisit(null, 0, null);
		
	    return true;
	}
	
	protected abstract Entry<String, ? extends SequentialAny> nextLine();
	
	protected abstract boolean hasNextLine();
	
	protected void reset() {
	}
	
	class EntriesIterator implements Iterator<Entry<String, ? extends SequentialAny>> {
		public boolean hasNext() {
		    return BaseShallowSequentialAny.this.hasNextLine();
		}
		public void remove() {
			throw new UnsupportedOperationException();
		}
        public Entry<String, ? extends SequentialAny> next() {
        	return BaseShallowSequentialAny.this.nextLine();
        }
	}

}
